package com.ly.heart.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.ly.common.core.domain.AjaxResult;
import com.ly.common.exception.CustomException;
import com.ly.common.utils.DateUtils;
import com.ly.common.utils.SecurityUtils;
import com.ly.common.utils.erp.ConstUtil;
import com.ly.common.utils.erp.SnowflakeIdWorker;
import com.ly.heart.mapper.KingBaseSettlementTypeMapper;
import com.ly.heart.service.IKingBaseSettlementTypeService;
import org.springframework.stereotype.Service;
import com.ly.heart.domain.KingBaseSettlementTypeEntity;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 金蝶-财务模块基础数据-结算方式Service业务层处理
 *
 * @author wutao
 * @date 2024-06-20
 */
@Service
public class KingBaseSettlementTypeServiceImpl extends ServiceImpl<KingBaseSettlementTypeMapper, KingBaseSettlementTypeEntity> implements IKingBaseSettlementTypeService {

    /**
     * 查询金蝶-财务模块基础数据-结算方式
     *
     * @param id 金蝶-财务模块基础数据-结算方式ID
     * @return 金蝶-财务模块基础数据-结算方式
     */
    @Override
    public KingBaseSettlementTypeEntity selectKingBaseSettlementTypeEntityById ( String id ) {
        return this.getById ( id );
    }

    /**
     * 查询金蝶-财务模块基础数据-结算方式列表
     *
     * @param kingBaseSettlementTypeEntity 金蝶-财务模块基础数据-结算方式
     * @return 金蝶-财务模块基础数据-结算方式
     */
    @Override
    public Page selectKingBaseSettlementTypeEntityList ( KingBaseSettlementTypeEntity kingBaseSettlementTypeEntity ) {
        LambdaQueryWrapper<KingBaseSettlementTypeEntity> queryWrapper = new LambdaQueryWrapper<> ();
        queryWrapper.eq ( KingBaseSettlementTypeEntity :: getIsUsed, ConstUtil.IS_USED_YES );
        queryWrapper.orderByDesc ( KingBaseSettlementTypeEntity :: getCreateTime );
        Page<KingBaseSettlementTypeEntity> page = this.page ( new Page<> ( kingBaseSettlementTypeEntity.getPage (), kingBaseSettlementTypeEntity.getLimit () ), queryWrapper );
        List<KingBaseSettlementTypeEntity> records = page.getRecords ();
        records.forEach ( settlement -> {
            String name = "1".equals ( settlement.getSettlementCategory () ) ? "现金" :
                    ( "2".equals ( settlement.getSettlementCategory () ) ? "支票" :
                            ( "3".equals ( settlement.getSettlementCategory () ) ? "商业承兑汇票" :
                                    ( "4".equals ( settlement.getSettlementCategory () ) ? "银行承兑汇票" :
                                            ( "5".equals ( settlement.getSettlementCategory () ) ? "本票" :
                                                    ( "6".equals ( settlement.getSettlementCategory () ) ? "汇兑" :
                                                            ( "7".equals ( settlement.getSettlementCategory () ) ? "信用证" :
                                                                    ( "8".equals ( settlement.getSettlementCategory () ) ? "委托收款" :
                                                                            ( "9".equals ( settlement.getSettlementCategory () ) ? "托收承付" :
                                                                                    ( "10".equals ( settlement.getSettlementCategory () ) ? "汇票" :
                                                                                            ( "11".equals ( settlement.getSettlementCategory () ) ? "银行汇票" :
                                                                                                    ( "12".equals ( settlement.getSettlementCategory () ) ? "电子支付" :
                                                                                                            ( "13".equals ( settlement.getSettlementCategory () ) ? "光票托收" :
                                                                                                                    ( "14".equals ( settlement.getSettlementCategory () ) ? "承兑交单" :
                                                                                                                            ( "15".equals ( settlement.getSettlementCategory () ) ? "远期付款交单" :
                                                                                                                                    ( "16".equals ( settlement.getSettlementCategory () ) ? "即期付款交单" :
                                                                                                                                            ( "17".equals ( settlement.getSettlementCategory () ) ? "票汇" :
                                                                                                                                                    ( "18".equals ( settlement.getSettlementCategory () ) ? "信用卡" :
                                                                                                                                                            ( "19".equals ( settlement.getSettlementCategory () ) ? "微信" : "支付宝" ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) );
            settlement.setSettlementCategory ( name );
            String bizClass = "1".equals ( settlement.getBizClassification () ) ? "现金业务" : ( "2".equals ( settlement.getBizClassification () ) ? "银行业务" : ( "3".equals ( settlement.getBizClassification () ) ? "票据业务" : "内部结算" ) );
            settlement.setBizClassification ( bizClass );
        } );
        page.setRecords ( records );
        return page;
    }

    /**
     * 新增金蝶-财务模块基础数据-结算方式
     *
     * @param kingBaseSettlementTypeEntity 金蝶-财务模块基础数据-结算方式
     * @return 结果
     */
    @Override
    public AjaxResult insertKingBaseSettlementTypeEntity ( KingBaseSettlementTypeEntity kingBaseSettlementTypeEntity ) {
        kingBaseSettlementTypeEntity.setId ( SnowflakeIdWorker.getInstance ().nextStringId () );
        int count = this.count ();
        kingBaseSettlementTypeEntity.setBizCode ( "JSFS".concat ( String.format ( "%08d", count ) ) );
        // 系统预设表示金蝶里预设，erp新增不是预设
        kingBaseSettlementTypeEntity.setIsPreset ( 2 );
        kingBaseSettlementTypeEntity.setCreateBy ( SecurityUtils.getLoginUser ().getUser ().getUserId () );
        kingBaseSettlementTypeEntity.setCreateTime ( DateUtils.getNowDate () );
        boolean save = this.save ( kingBaseSettlementTypeEntity );
        if ( ! save ) {
            throw new CustomException ( "新增结算方式失败" );
        }
        return AjaxResult.success ( "新增结算方式成功" );
    }

    /**
     * 修改金蝶-财务模块基础数据-结算方式
     *
     * @param kingBaseSettlementTypeEntity 金蝶-财务模块基础数据-结算方式
     * @return 结果
     */
    @Override
    public AjaxResult updateKingBaseSettlementTypeEntity ( KingBaseSettlementTypeEntity kingBaseSettlementTypeEntity ) {
        kingBaseSettlementTypeEntity.setUpdateBy ( SecurityUtils.getLoginUser ().getUser ().getUserId () );
        kingBaseSettlementTypeEntity.setUpdateTime ( DateUtils.getNowDate () );
        boolean b = this.updateById ( kingBaseSettlementTypeEntity );
        if ( ! b ) {
            throw new CustomException ( "修改结算方式失败！" );
        }
        return AjaxResult.success ( "修改结算方式成功" );
    }

    /**
     * 批量删除金蝶-财务模块基础数据-结算方式
     *
     * @param ids 需要删除的金蝶-财务模块基础数据-结算方式ID
     * @return 结果
     */
    @Override
    public AjaxResult deleteKingBaseSettlementTypeEntityByIds ( String[] ids ) {
        List<KingBaseSettlementTypeEntity> list = this.list ( new LambdaQueryWrapper<KingBaseSettlementTypeEntity> ().in ( KingBaseSettlementTypeEntity :: getId, ids ) );
        List<String> idList = list.stream ().filter ( settlementTypeEntity -> settlementTypeEntity.getIsDel () == 1 ).map ( KingBaseSettlementTypeEntity :: getId ).collect ( Collectors.toList () );
        StringBuilder stringBuilder = new StringBuilder ( "【" );
        list.forEach ( settlementTypeEntity -> {
            if ( settlementTypeEntity.getIsDel () == 2 ) {
                stringBuilder.append ( settlementTypeEntity.getTitle () );
            }
        } );
        stringBuilder.append ( "】" );
        boolean update = this.update ( new LambdaUpdateWrapper<KingBaseSettlementTypeEntity> ().
                set ( KingBaseSettlementTypeEntity :: getIsUsed, ConstUtil.IS_USED_NO ).
                set ( KingBaseSettlementTypeEntity :: getUpdateBy, SecurityUtils.getLoginUser ().getUser ().getUserId () ).
                set ( KingBaseSettlementTypeEntity :: getUpdateTime, DateUtils.getNowDate () ).
                in ( KingBaseSettlementTypeEntity :: getId, idList ) );
        if ( ! update ) {
            throw new CustomException ( "删除结算方式失败！" );
        }
        String str = "删除结算方式成功";
        if ( list.size () > 0 ) {
            str.concat ( "，其中" ).concat ( stringBuilder.toString () ).concat ( "被引用，不可删除" );
        }
        return AjaxResult.success ( str );
    }

    /**
     * 删除金蝶-财务模块基础数据-结算方式信息
     *
     * @param id 金蝶-财务模块基础数据-结算方式ID
     * @return 结果
     */
    @Override
    public AjaxResult disable ( String id ) {
        KingBaseSettlementTypeEntity baseSettlementTypeEntity = this.getById ( id );
        boolean update = this.update ( new LambdaUpdateWrapper<KingBaseSettlementTypeEntity> ().
                set ( KingBaseSettlementTypeEntity :: getIsDisabled, baseSettlementTypeEntity.getIsDisabled () == 1 ? 2 : 1 ).
                set ( KingBaseSettlementTypeEntity :: getUpdateBy, SecurityUtils.getLoginUser ().getUser ().getUserId () ).
                set ( KingBaseSettlementTypeEntity :: getUpdateTime, DateUtils.getNowDate () ).
                eq ( KingBaseSettlementTypeEntity :: getId, id ) );
        if ( ! update ) {
            throw new CustomException ( baseSettlementTypeEntity.getIsDisabled () == 1 ? "禁用结算方式失败！" : "解禁结算方式失败！" );
        }
        return AjaxResult.success ( baseSettlementTypeEntity.getIsDisabled () == 1 ? "禁用结算方式成功" : "解禁结算方式成功" );
    }
}
